using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ACM.cmsarea08.Controls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:SuggestionTextBox runat=server></{0}:SuggestionTextBox>")]
    public class SuggestionTextBox : TextBox
    {

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Sql
        {
            get
            {
                String s = (String)ViewState["Sql"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["Sql"] = value;
            }
        }


        public string Class
        {
            get
            {
                String s = (String)ViewState["Class"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["Class"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string DynPart
        {
            get
            {
                String s = (String)ViewState["DynPart"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["DynPart"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int MinChars
        {
            get
            {
                if (ViewState["MinChars"] == null)
                    return 3;
                else
                    return (int)ViewState["MinChars"];
            }

            set
            {
                ViewState["MinChars"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int NumberSuggestions
        {
            get
            {
                if (ViewState["NumberSuggestions"] == null)
                    return 5;
                else
                    return (int)ViewState["NumberSuggestions"];
            }

            set
            {
                ViewState["NumberSuggestions"] = value;
            }
        }
        
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            string suggestionboxurl = "/template/plugins/SuggestionTextBox/Observer.js";//Page.ClientScript.GetWebResourceUrl(typeof(EmbedInput), "ACM.cmsarea08.Controls.Resources.EmbedObject.js");
            string suggestionboxurl2 = "/template/plugins/SuggestionTextBox/Autocompleter.js";//Page.ClientScript.GetWebResourceUrl(typeof(EmbedInput), "ACM.cmsarea08.Controls.Resources.EmbedObject.js");
            string suggestionboxurl3 = "/template/plugins/SuggestionTextBox/Autocompleter.Request.js";//Page.ClientScript.GetWebResourceUrl(typeof(EmbedInput), "ACM.cmsarea08.Controls.Resources.EmbedObject.js");
            string csssuggestionboxurl = "/template/plugins/SuggestionTextBox/Autocompleter.css";//Page.ClientScript.GetWebResourceUrl(typeof(EmbedInput), "ACM.cmsarea08.Controls.Resources.EmbedObject.css");
            if(this.Page is ACM.cmsarea08.Core.MainPage) {
            	((ACM.cmsarea08.Core.MainPage)this.Page).RegisterMooTools();
                ((ACM.cmsarea08.Core.MainPage)this.Page).RegisterCustomJavascript(suggestionboxurl);
                ((ACM.cmsarea08.Core.MainPage)this.Page).RegisterCustomJavascript(suggestionboxurl2);
                ((ACM.cmsarea08.Core.MainPage)this.Page).RegisterCustomJavascript(suggestionboxurl3);
                ((ACM.cmsarea08.Core.MainPage)this.Page).RegisterCustomCss(csssuggestionboxurl);
            } else {
            	//includi lo script... mootools..
            	if(this.Page.Header!=null) {
            		System.Web.UI.HtmlControls.HtmlHead myhead = this.Page.Header;
					bool foundMo = false;
					bool foundEb = false;
					bool foundEb2 = false;
					bool foundEb3 = false;
                    bool foundEbc = false;
					for(int i = 0; i <myhead.Controls.Count;i++) {
						if(myhead.Controls[i] is LiteralControl) {
							if(((LiteralControl)myhead.Controls[i]).Text.ToLower().IndexOf("mootools")!=-1) {
								foundMo=true;
							}
							if(((LiteralControl)myhead.Controls[i]).Text.ToLower().IndexOf("Observer.js")!=-1) {
								foundEb=true;
							}
							if(((LiteralControl)myhead.Controls[i]).Text.ToLower().IndexOf("Autocompleter.js")!=-1) {
								foundEb2=true;
							}
							if(((LiteralControl)myhead.Controls[i]).Text.ToLower().IndexOf("Autocompleter.Request.js")!=-1) {
								foundEb3=true;
							}
                    		
                            if (((LiteralControl)myhead.Controls[i]).Text.ToLower().IndexOf("Autocompleter.css") != -1)
                            {
                                foundEbc = true;
                            }

						}
					}
					if(!foundMo) {
						LiteralControl ct = new LiteralControl("<script language=\"javascript\" type=\"text/javascript\" src=\"/template/plugins/mootools-1.js\"></script>");
						myhead.Controls.Add(ct);			
					}            		
					if(!foundEb) {
						LiteralControl ct = new LiteralControl("<script language=\"javascript\" type=\"text/javascript\" src=\""+suggestionboxurl+"\"></script>");
						myhead.Controls.Add(ct);			
					}
					if(!foundEb2) {
						LiteralControl ct = new LiteralControl("<script language=\"javascript\" type=\"text/javascript\" src=\""+suggestionboxurl2+"\"></script>");
						myhead.Controls.Add(ct);			
					}
					if(!foundEb3) {
						LiteralControl ct = new LiteralControl("<script language=\"javascript\" type=\"text/javascript\" src=\""+suggestionboxurl3+"\"></script>");
						myhead.Controls.Add(ct);			
					}                    
                    if (!foundEbc)
                    {
                        LiteralControl ct = new LiteralControl("<style type=\"text/css\" media=\"screen\">\n"+
                            "<!--\n"+
                            "@import url(\""+csssuggestionboxurl+"\");\n"+
                            "-->\n</style>");
                        myhead.Controls.Add(ct);
                    }             		
            		
            	}
            }        	
        }
        
        

        protected override void Render(HtmlTextWriter writer)
        {
            
            writer.Write("<span id=\"" + this.ClientID + "_cont\" "+
        	             (this.Class!=""?("class=\"" + this.Class+"\""):"")+">");
            base.Render(writer);
            writer.Write("</span>");
            
            writer.Write("<script language=\"javascript\" type=\"text/javascript\" defer=\"defer\">\n");
            writer.Write("window.addEvent(\"domready\",function(){");
            writer.Write(" var mioSug" + this.ClientID +" = new Autocompleter.Request.JSON( " +
                         "'" + this.ClientID + "',\n '/SuggestionHandler.aspx" +
						 "?query=" +
                         HttpUtility.UrlEncode(this.Sql.Replace(this.DynPart,this.ClientID)).Replace("'",@"\'")+
                         "&dynpart=" +this.ClientID.Replace("'",@"\'") + "',\n {" +            
                         " 'postVar': 	'"+this.ClientID+"', " +
                         " 'minLength': " + this.MinChars + "});\n");
			writer.Write("});");            
            writer.Write("\n</script>");
//						 "?query=" +
//                         HttpUtility.UrlEncode(this.Sql.Replace(this.DynPart,this.ClientID)).Replace("'",@"\'")+
//                         "&dynpart=" +this.ClientID.Replace("'",@"\'") + "',\n {" +            
            
        }
        
    }
}
